﻿Imports System.Data.OleDb
Imports System.Xml  'for generating xml
Imports System.ServiceModel.Syndication   'added via Properties System.ServiceModel.Web




Public Class rss

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'identify that the content of this page is not html type
        Response.Clear()
        Response.ContentType = "application/rss+xml"
        'didn't work at first - had to also add System.ServiceModel to the properties
        Dim myFeed As SyndicationFeed = New SyndicationFeed("3D Buzz - Projection Mapping", "5 most recently added artists", New Uri(okURL("artists.aspx")))
        myFeed.Language = "en-us"      'specify language - English


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("3dBuzzDatabase").ConnectionString)
        Dim SqlString As String = "SELECT TOP 5 Artists.[ID], Artists.[Nickname], Artists.[Country], Artists.[WebURL] FROM Artists ORDER BY Artists.[ID] DESC"        'last 5 artists added - might not be most recent activity as not datestamped
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        oleDbConn.Open()
        Dim dataReader = cmd.ExecuteReader()    'because we are returning results
        ' dataReader.Read()

        'selected top 6, but displays top 5 - can't be bothere to check the while, probably should be a different loop through as you lose
        'the first one, quicker to find top 6 and display top 5 - sorry!  but it works.
        '13 Jan - sorted - had an extra dataread.!
        'add data items to an array that will go to the feed
        Dim myItems As New List(Of SyndicationItem)
        While dataReader.Read()   'whilst still records being returned
            Dim sItem As New SyndicationItem
            sItem.Title = makeRssTxt(dataReader.Item("Nickname"))
            sItem.Summary = makeRssTxt(dataReader.Item("Country") & dataReader.Item("WebURL"))
            'sItem.Links.Add(dataReader.Item("WebURL"))
            myItems.Add(sItem)  'add the syndication item to the array
        End While

        myFeed.Items = myItems



        Dim feedWriter As XmlWriter = XmlWriter.Create(Response.OutputStream)
        Dim rssFormatter = New Rss20FeedFormatter(myFeed)         'format to rss2.0

        'flush the data out
        rssFormatter.WriteTo(feedWriter)    'the xml writer object that writes to the output string
        feedWriter.Close()

        oleDbConn.Close()
        oleDbConn.Dispose()

    End Sub

    'function from tutor S Bennett video feed
    Private Function okURL(ByVal urlStr As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(urlStr)
    End Function

    Private Function makeRssTxt(ByVal someString As String) As TextSyndicationContent
        Return TextSyndicationContent.CreatePlaintextContent(someString)

    End Function

    Private Function makeLink(ByVal someLocation As String) As SyndicationLink
        Return SyndicationLink.CreateAlternateLink(New Uri(someLocation))
    End Function
End Class